package io.hepu.robotize.auth;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Shiros {

    // 加密算法
    static final String HASH_ALGORITHMNAME = "MD5";

    // 加密次数
    static final int HASH_ITERATIONS = 3;

    /**
     * Binds the specified {@code value} to current session.
     *
     * @param key attribute key
     * @param obj attribute value
     */
    public static void setAttribute(String key, Object obj) {
        SecurityUtils.getSubject().getSession().setAttribute(key, obj);
    }

    /**
     * Returns the object bound to current session.
     *
     * @param key attribute key
     */
    public static Object getAttribute(String key) {
        return SecurityUtils.getSubject().getSession().getAttribute(key);
    }

    /**
     * Returns current principal object bound to current session.
     *
     * @return principal
     */
    public static Object getPrincipal() {
        return SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * Returns the encoded string from specified session.
     *
     * @param session Session
     * @return String
     */
    public static String serialize(Session session) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(session);
            return Base64.encodeToString(bos.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException("serialize session error", e);
        }
    }

    /**
     * Returns the session deserialize from session string.
     *
     * @param encoded encoded string
     * @return Session
     */
    public static Session deserialize(String encoded) {
        try (ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(encoded));
             ObjectInputStream ois = new ObjectInputStream(bis)) {
            return (Session) ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException("deserialize session error", e);
        }
    }

    /**
     * Creates an algorithmName-specific hash of the specified source using the given salt a total of hashIterations times.
     *
     * @param algorithmName  the MessageDigest algorithm name.
     * @param source         the object to be hashed.
     * @param salt           a salt used to compute the hash.
     * @param hashIterations the number of times the source argument hashed for attack resiliency.
     * @return String
     */
    public static String getSimpleHash(String algorithmName, String source, String salt, int hashIterations) {
        SimpleHash simpleHash = new SimpleHash(algorithmName, source, salt, hashIterations);
        return simpleHash.toString();
    }

    /**
     * Generates an MD5 Hash (RFC 1321) from a given input source with an optional salt and hash iterations.
     *
     * @param source         the object to be hashed.
     * @param salt           a salt used to compute the hash.
     * @param hashIterations the number of times the source argument hashed for attack resiliency.
     * @return String
     */
    public static String getMd5Hash(String source, String salt, int hashIterations) {
        Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);
        return md5Hash.toString();
    }

    /**
     * 获取加密后的密码
     *
     * @param original 明文密码
     * @return java.lang.String
     */
    public static String encrypt(String original, String salt) {
        return getSimpleHash(HASH_ALGORITHMNAME, original, salt, HASH_ITERATIONS);
    }

}
